package GUILayer;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import CtrLayer.PersonCtr;
import ModelLayer.Employee;


/**
 * LoginGUI.
 * This class contains all userloginbuttons.
 * these buttons are created from info from the database.
 * it also contains the 'close program' button which shuts down the program.
 * 31.05.2012
 * @author Lindgren
 * @Version 0.1
 */
public class LoginGUI {



	private static LoginGUI instance;

	private JPanel loginGUIPanel;

	private static int tabIndex = 1;

	private PersonCtr personCtr = PersonCtr.getInstance();

	private static HashMap<String, Employee> employees = new HashMap<String, Employee>(); //employee object is added to this map when a user logs in.

	private ArrayList<Employee> allEmployees ;


	/**
	 * Singleton.
	 * Return: instance.
	 */
	public static LoginGUI getInstance()
	{
		if(instance==null)
		{
			instance = new LoginGUI();
			System.out.println("LoginGUI instance created: "+instance);
		}
		System.out.println("LoginGUI instance returned: "+instance);
		return instance;
	}


	/**
	 * Singleton 2/2 - new LoginGUI().
	 */
	private LoginGUI() {
		allEmployees=getAllEmployees();
		initialize();
		System.out.println("LoginGUI initialized!");
	}

	/**
	 * Initializes the loginGUI
	 */
	private void initialize() {

		System.out.println("LoginGUI initializing");

		loginGUIPanel = new JPanel();
		//((ImageIcon) loginGUIPanel).Icon(new ImageIcon(MainGUI.class.getResource("/GUILayer/Images/logo.jpg")));
		loginGUIPanel.setToolTipText("loginGUIPanel");

		loginGUIPanel.setName("Login Menu");

		MainFrame.getTabbedPane().add(loginGUIPanel);
		System.out.println("- added to loginGUIPanel: "+loginGUIPanel);
		loginGUIPanel.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel panel = new JPanel();
		loginGUIPanel.add(panel);
		panel.setLayout(new GridLayout(0, 2, 0, 0));

		JPanel mngrPanel = new JPanel();
		panel.add(mngrPanel);
		mngrPanel.setToolTipText("Managers Login");
		mngrPanel.setBorder(new TitledBorder(null, "Managers", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		mngrPanel.setLayout(new GridLayout(1, 0, 3, 3));


		JPanel empPanel = new JPanel();
		empPanel.setLayout(new GridLayout(0, 3, 3, 3));
		panel.add(empPanel);
		empPanel.setToolTipText("Employees Login");
		empPanel.setBorder(new TitledBorder(null, "Employees", TitledBorder.LEADING, TitledBorder.TOP, null, null));


		// creates all login buttons
		for (final Employee emp : allEmployees) {

			if( emp.getJobTitle().equals("Owner") || emp.getJobTitle().equals("Manager") ) {

				// new button is created with the managers login name on it.
				JButton btnEmp = new JButton(emp.getLogin());

				// a new ActionListener is created and the method 'openMainGUI(emp)' is added to it.
				ActionListener actL = new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						System.out.println("ActionListener activated!");
						openMainGUI(emp);
					}
				};

				// the ActionListener is added to the button.
				btnEmp.addActionListener(actL);

				// button is added to mngrPanel.
				mngrPanel.add(btnEmp);
				System.out.println("EmployeeButton Created: "+btnEmp.getText()+", Employee Id: "+emp.getEmpId());

			}
			else{
				// new button is created with the employees login name on it.
				JButton btnEmp = new JButton(emp.getLogin());

				// a new ActionListener is created and the method 'openMainGUI(emp)' is added to it.
				ActionListener actL = new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						System.out.println("ActionListener activated!");
						openMainGUI(emp);
					}
				};

				// the ActionListener is added to the button.
				btnEmp.addActionListener(actL);

				// button is added to empPanel.
				empPanel.add(btnEmp);
				System.out.println("EmployeeButton Created: "+btnEmp.getText()+", Employee Id: "+emp.getEmpId());
			}
		}


		JButton btnBackup = new JButton("Backup");
		btnBackup.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//TODO action on backup button
			}
		});
		empPanel.add(btnBackup);

		JPanel panel_1 = new JPanel();
		panel_1.setForeground(Color.WHITE);
		loginGUIPanel.add(panel_1);
		panel_1.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2);
		panel_2.setLayout(new GridLayout(1, 0, 0, 0));

		JPanel panel_3 = new JPanel();
		panel_1.add(panel_3);
		panel_3.setLayout(new GridLayout(0, 4, 0, 0));

		JButton btnCloseProgram = new JButton("Close Program");
		panel_3.add(btnCloseProgram);
		btnCloseProgram.setForeground(Color.WHITE);
		btnCloseProgram.setBackground(Color.RED);

		Component glue_2 = Box.createGlue();
		panel_3.add(glue_2);

		Component glue_1 = Box.createGlue();
		panel_3.add(glue_1);

		Component glue = Box.createGlue();
		panel_3.add(glue);

		JPanel panel_4 = new JPanel();
		panel_1.add(panel_4);
		panel_4.setLayout(new GridLayout(1, 0, 0, 0));
		btnCloseProgram.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("Shutting down MainFrame");
				try {
					MainFrame.getFrmTheIrishHouse().dispose();
				} catch (Exception e1) {
					System.err.println("Shutting down mainframe failed");
					e1.printStackTrace();
				}				
			}
		});
	}

	/**
	 * Creates a userFrame for the user that logs in and opens the mainGUI
	 */
	private void openMainGUI(Employee employee)
	{
		try {
			System.out.println("LoginGUI actionBtnLogin pressed");

			if (  employees.get( employee.getLogin() ) == null) //if the employee object is NOT found in the map, the employee will be logged in.
			{

				System.out.println("LoginGUI: logging " + employee.getLogin()+ " in");

				employees.put(employee.getLogin(), employee); //adds employee to HashMap.

				UserFrame userFrame = new UserFrame(employee);
				System.out.println("LoginGUI userFrame "+employee.getLogin()+" created: "+userFrame);

				MainFrame.getUserFrames().put(employee.getLogin(), userFrame);
				System.out.println("LoginGUI userFrame "+employee.getLogin()+" mapped; "+MainFrame.getUserFrames().get(employee.getLogin()));

				MainFrame.getTabbedPane().add(MainFrame.getUserFramePanels().get(employee.getLogin()));
				System.out.println("LoginGUI userFramePanel "+MainFrame.getUserFramePanels().get(employee.getLogin()));
				System.out.println("- added to tabbedPane: "+MainFrame.getTabbedPane());

				MainFrame.getUserFramePanels().get(employee.getLogin()).add(MainFrame.getMainGUIPanels().get(employee.getLogin()));
				System.out.println("LoginGUI mainGUIPanel "+MainFrame.getMainGUIPanels().get(employee.getLogin()));
				System.out.println("- added to userFramePanel: "+MainFrame.getUserFramePanels().get(employee.getLogin()));
				System.out.println("LoginGUI "+employee.getLogin()+" is logged in");
				System.out.println("");

				MainFrame.getTabbedPane().setSelectedIndex(tabIndex); //sets focus on new tab.
				tabIndex ++;
			}
			else
			{
				System.err.println("User "+employee.getLogin()+" allready logged in");
			}
		} catch (Exception e) {
			System.err.println("LogIn "+employee.getLogin()+" failed");
			e.printStackTrace();
		}

	}


	/**
	 * @return the tabIndex
	 */
	public static int getTabIndex() {
		return tabIndex;
	}


	/**
	 * @param tabIndex the tabIndex to set
	 */
	public static void setTabIndex(int tabIndex) {
		LoginGUI.tabIndex = tabIndex;
	}



	/**
	 * @return the employees HashMap that contains employees who are logged in.
	 */
	public static HashMap<String, Employee> getEmployees() {
		return employees;
	}


	/**
	 * @return the allEmployees ArrayList generated from employees stored in the database.
	 */
	private ArrayList<Employee> getAllEmployees() {		
		return personCtr.getAllEmployees();
	}
}
